package com.tangly.codeing.service.impl;

import com.tangly.codeing.dao.SysPrivilegeDao;
import com.tangly.codeing.model.SysPrivilege;
import com.tangly.codeing.model.vo.SysPrivilegeVo;
import com.tangly.codeing.service.SysPrivilegeService;
import com.tangly.codeing.utils.PageInfo;
import com.tangly.codeing.utils.ReturnT;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 权限表
 *
 * @author tangbing
 * @date 2019-05-11
 */
@Service
public class SysPrivilegeServiceImpl implements SysPrivilegeService {

    @Resource
    private SysPrivilegeDao sysPrivilegeDao;

    /**
     * 新增
     */
    @Override
    public ReturnT<String> insert(SysPrivilege sysPrivilege) {

        // valid
        if (sysPrivilege == null) {
            return new ReturnT<>(ReturnT.FAIL_CODE, "必要参数缺失");
        }

        sysPrivilegeDao.insert(sysPrivilege);
        return ReturnT.SUCCESS;
    }

    /**
     * 删除
     */
    @Override
    public ReturnT<String> delete(Long id) {
        int ret = sysPrivilegeDao.delete(id);
        return ret > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
    }

    /**
     * 批量删除
     */
    @Override
    public ReturnT<String> batchDelete(List<String> ids) {
        int ret = sysPrivilegeDao.batchDelete(ids);
        return ret > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
    }

    /**
     * 更新
     */
    @Override
    public ReturnT<String> update(SysPrivilege sysPrivilege) {
        int ret = sysPrivilegeDao.update(sysPrivilege);
        return ret > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
    }

    /**
     * Load查询
     */
    @Override
    public SysPrivilege load(Long id) {
        return sysPrivilegeDao.load(id);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo pageList(PageInfo pageInfo) {

        List<SysPrivilege> pageList = sysPrivilegeDao.pageList(pageInfo);
        int totalCount = sysPrivilegeDao.pageListCount(pageInfo);

        pageInfo.setResultObject(pageList);
        pageInfo.setTotalRecord(totalCount);
        return pageInfo;
    }

    @Override
    public List<SysPrivilege> findAll() {
        return sysPrivilegeDao.findAll();
    }

    @Override
    public List<SysPrivilegeVo> findAllRoot() {
        List<SysPrivilegeVo> rootList = sysPrivilegeDao.findAllRoot();
        getAllChildNode(rootList);
        return rootList;
    }

    @Override
    public List<SysPrivilege> findByRoleIds(List<Long> roleIds) {
        return sysPrivilegeDao.findByRoleIds(roleIds);
    }

    List<SysPrivilegeVo> getAllChildNode(List<SysPrivilegeVo> rootList) {
        for (SysPrivilegeVo sysPrivilegeVo : rootList) {
            List<SysPrivilegeVo> list = sysPrivilegeDao.findDownById(sysPrivilegeVo.getId());
            if (list.size() != 0) {
                sysPrivilegeVo.setChildren(list);
                getAllChildNode(sysPrivilegeVo.getChildren());
            }
        }
        return rootList;
    }

    @Override
    public Set<String> findPrivilegeIdByRoleId(String roleId) {
        return sysPrivilegeDao.findPrivilegeIdByRoleId(roleId);
    }
}
